//$Id: EhCache.java 10716 2006-11-03 19:05:11Z max.andersen@jboss.com $
/**
 * Copyright 2003-2006 Greg Luck, Jboss Inc
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.business.java2cache.ehcache;

import com.business.java2cache.core.Cache;
import com.business.java2cache.core.CacheConstans;
import com.business.java2cache.util.CacheException;
import net.sf.ehcache.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * EHCache
 */
public class EhCache implements Cache {

    private final static Logger log = LoggerFactory.getLogger(EhCache.class);

    private net.sf.ehcache.Cache cache;

    EhCache(net.sf.ehcache.Cache cache) {
        this.cache = cache;
    }

    /**
     * Get an item from the cache, nontransactionally
     *
     * @param region cache region
     * @param key    cache key
     * @return the cached object or null
     */
    @Override
    public Object get(String region, String key) throws CacheException {
        try {
            if (key == null)
                return null;
            else {
                if (region == null)
                    region = CacheConstans.ehcache_defaultRegion;

                net.sf.ehcache.Cache ehcache = getCache(region);
                Element element = ehcache.get(key);
                if (element != null)
                    return element.getObjectValue();
            }
            return null;
        } catch (net.sf.ehcache.CacheException e) {
            throw new CacheException(e);
        }
    }

    /**
     * Add an item to the cache, nontransactionally, with
     * failfast semantics
     *
     * @param region
     * @param key     cache key
     * @param value   cache value
     * @param seconds cache Expiration time
     */
    @Override
    public void set(String region, String key, Object value, int seconds) throws CacheException {
        try {
            if (region == null)
                region = CacheConstans.ehcache_defaultRegion;
            if (value == null)//vaule==null delete
                remove(region, key);
            else {
                net.sf.ehcache.Cache ehcache = getCache(region);
                Element element = new Element(key, value);
                ehcache.put(element);
            }

        } catch (IllegalArgumentException e) {
            throw new CacheException(e);
        } catch (IllegalStateException e) {
            throw new CacheException(e);
        } catch (net.sf.ehcache.CacheException e) {
            throw new CacheException(e);
        }
    }


    /**
     * @param key Cache key
     *            Remove an item from the cache
     */
    @Override
    public void remove(String region, Object key) throws CacheException {
        if (region == null)
            region = CacheConstans.ehcache_defaultRegion;
        if (key instanceof List) {//list
            List keys = (List) key;
            net.sf.ehcache.Cache ehcache = getCache(region);
            ehcache.removeAll(keys);
        } else {//string
            try {
                net.sf.ehcache.Cache ehcache = getCache(region);
                ehcache.remove(key.toString());
            } catch (IllegalStateException e) {
                throw new CacheException(e);
            } catch (net.sf.ehcache.CacheException e) {
                throw new CacheException(e);
            }
        }

    }

    /**
     * Clear the cache
     */
    @Override
    public void clear(String region) throws CacheException {
        try {
            if (region == null)
                region = CacheConstans.ehcache_defaultRegion;
            net.sf.ehcache.Cache ehcache = getCache(region);
            ehcache.removeAll();
        } catch (IllegalStateException e) {
            throw new CacheException(e);
        } catch (net.sf.ehcache.CacheException e) {
            throw new CacheException(e);
        }
    }


    /**
     * update exprie time
     * ehcache不能更新时间，直接返回值
     *
     * @param key
     * @param seconds
     */
    @Override
    public Object exprie(String region, String key, int seconds) {
        return get(region, key);
    }

    //###################################

    private net.sf.ehcache.Cache getCache(String region) {
        return this.cache;
    }
}